*********************************
* READ ME
*********************************
* Part of our survey instrument was a conjoint experiment, iterated three times. Our replication materials include two data files. "data wide.dta" is structure so that each respondent has one row in the data, with different columns/variables for each iteration. "data long.dta" is structured so that there is are four rows per respondent--one for each iteration of the conjoint. You will find that the code below uses "data long.dta" when analyzing the conjoint and "data wide.dta" when analyzing other aspects of the instrument.

* For the code to run correctly, set your working directory here:
cd "C:\Users\abcde\Desktop\"

* Uncomment these two lines if necessary to install required packages:
*ssc install conjoint, replace all
*ssc install coefplot, replace all
*ssc install blindschemes, replace all
set scheme plotplainblind 

* This line should future-proof our code, so that future versions of Stata run the same as Stata 18. Be aware, however, that if the packages installed above are updated in the future, they may cease to work the same as when these materials were assembled.
version 18

* We made a few manual edits to the "conjoint" package to enable greater control over the final appearance of generated figures. In particular, our edits (1) allow us to use the xtitle() argument with the "conjoint" command and (2) allow us to suppress conjoint's automatic xline() argument when argument h0() is not explicitly specificed. It is NOT necessary to make those edits for this code to run. Just be aware that the figures you generate may have a different title for the horizontal axis than you find in our publication, and figures you generate may also have a vertical line that is absent from our figures.

* If you want your figures to look exactly like ours, uncomment. This is permanent unless you set it to a different font later
*graph set window fontface "Gill Sans MT"

*********************************
* END OF COMMENTS. Be sure you set your working directory and installed required packages above.
*********************************


* ***************** Figure 2
use "data wide.dta", clear

* create labels
gen t_agree = vig_policyagree
lab def t_agree 1 "1: Strongly oppose" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6: Strongly support", modify
lab val t_agree t_agree
* run the figure's underlying model
quietly reg vig_response t_agree vig_divisive i.vig_constold
* use margins command to calculate predictions
margins vig_constold, at(t_agree=(1(1)6))
* plot the results
marginsplot, ytitle("Predicted support for invalidation") legend( order( 2 "1896 provision" 1 "2016 provision" ) pos(6) col(2) ) title("") xscale( r(0.5 6.5) ) xtitle( "Respondent's prior agreement with policy" )


* ***************** Figure 3
use "data wide.dta", clear

* Respondents viewed several iterations of the conjoint experiment. This figure aggregates respondents' views across iterations. To prepare this figure, we create two matrices that gather the results from various iterations into a usable form. First: A matrix that gathers respondents' views about judicial invalidation under the US Constitution or under the hypothetical state constitutions. Second: A similar matrix, but for respondents' views about the appropriate way to interpret each constitution.

* Make a measure of each respondent's average views on state philosophy and invalidation across the 4 iterations
gen t_state_net = ( state1_net + state2_net + state3_net + state4_net ) / 4
gen t_state_invalidate = ( state1_invalidation + state2_invalidation + state3_invalidation + state4_invalidation ) / 4

* Create some null variables that we'll populate below.
gen c_scotus = .
gen c_state = .
lab var c_scotus "US Constitution"
lab var c_state "State constitutions"

* Run this code twice, once for each matrix we wish to create.
forvalues i = 1/2{
	* On the first loop, we'll create variables and a matrix with views about invalidation
	if `i' == 1{
		replace c_scotus = scotus_invalidate
		replace c_state = t_state_invalidate
		local matrixName "coef_invalidate"
	}
	
	* On the second loop, we'll create variables and a matrix with views about judicial philosophy
	if `i' == 2{
		replace c_scotus = scotus_net
		replace c_state = t_state_net
		local matrixName "coef_phil"
	}
	
	* Now for the code
	
	* Create a new matrix
	matrix `matrixName' = J(3,2,.)
	
	* Name its rows and colums. Names in plots will use variable labels previously defined.
	matrix rownames `matrixName' = mean lower95 upper95
	matrix colnames `matrixName' = c_scotus c_state
	
	* Iterate through each variable we wish to include in the matrix. Loop vars in the same order we defined colnames above.
	local i 0
	foreach v of var c_scotus c_state {
		local ++ i
		quietly sum `v'
		* after "sum", mean is in r(mean) and sd is in r(sd), but we need to divide sd by sqrt(n) to get the se and calculate CI
		local rmean r(mean)
		local lower r(mean) - 1.96*( r(sd)/sqrt(r(N)) )
		local upper r(mean) + 1.96*( r(sd)/sqrt(r(N)) )
		* add to the matrix
		matrix `matrixName'[1,`i'] = `rmean' \ `lower' \ `upper'
	}
	* check our work
	matrix list `matrixName'
}

* Now that we've compiled all the points we wish to plot, prepare each panel of Figure 3.
* Left panel
coefplot matrix(coef_invalidate), ci((2 3)) vertical title( "Support for judicial review" ) yscale(r(3 3.2)) ylabel(3(0.1)3.2) name(coefinv, replace) nodraw
* Right panel
coefplot matrix(coef_phil), ci((2 3)) vertical title( "Support for living relative to original interpretation" ) yscale(r(0 0.6)) ylabel(0(0.2)0.6) name(coefphi, replace) nodraw
* Combine them
graph combine coefinv coefphi, col(2) scale(2.2) ysize(2) xsize(5.5)


* ***************** Figure 4 and 5
use "data long.dta", clear

* Figure 4
quietly reg state_net state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match
quietly sum state_net if e(sample)
local ymean = r(mean)
conjoint state_net state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph

* Figure 5
quietly reg state_invalidation state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match
quietly sum state_invalidation if e(sample)
local ymean = r(mean)
conjoint state_invalidation state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph


* ***************** Supplement

* Table S2
use "data wide.dta", clear
sum scotus_original scotus_living scotus_net scotus_invalidate usconst_selfish usconst_amendmore usconst_outdated usconst_visionary usconst_factor 

* Table S3
use "data long.dta", clear
xtset id iteration
xtsum state_original state_living state_net state_invalidation state_selfish state_amendmore state_outdated state_visionary state_factor

* Table S4
use "data wide.dta", clear
sum policy_teenabort policy_banguns policy_crtschools policy_medicaid policy_trans policy_gunfree policy_affirmative

* Table S5
sum vig_policyagree vig_divisive vig_constold vig_response

* Table S6
use "data long.dta", clear
reg state_original state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
reg state_living state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
reg state_net state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
reg state_invalidation state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )

* Table S7
oprobit state_original state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
oprobit state_living state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
oprobit state_net state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
oprobit state_invalidation state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )

* Figure S1a
quietly reg state_original state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match
quietly sum state_original if e(sample)
local ymean = r(mean)
conjoint state_original state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph h0(`ymean')

* Figure S1b
gen t_twoparty = 0 if r_pid3 == 1
replace t_twoparty = 1 if r_pid3 == 3
lab def t_twoparty 0 "Ds" 1 "Rs", modify
lab val t_twoparty t_twoparty
conjoint state_original state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph notable subgroup( t_twoparty )

* Figure S2a
quietly reg state_living state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match
quietly sum state_living if e(sample)
local ymean = r(mean)
conjoint state_living state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph h0(`ymean')

* Figure S2b
conjoint state_living state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph notable subgroup( t_twoparty )

* Figure S3a
quietly reg state_net state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match
quietly sum state_net if e(sample)
local ymean = r(mean)
conjoint state_net state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph

* Figure S3b
conjoint state_net state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph notable subgroup( t_twoparty )

* Figure S4a
quietly reg state_invalidation state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match
quietly sum state_invalidation if e(sample)
local ymean = r(mean)
conjoint state_invalidation state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph

* Figure S4b
conjoint state_invalidation state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph notable subgroup( t_twoparty )

* Table S8
reg state_outdated state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
reg state_amendmore state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
reg state_selfish state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
reg state_visionary state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )

* Figure S5a
quietly reg state_factor state_constage state_amendage state_amended state_constlength state_policy state_pid_match, vce( cluster id )
quietly sum state_factor if e(sample)
local ymean = r(mean)
conjoint state_factor state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph notable h0(`ymean')

* Figure Sb5
conjoint state_factor state_constyear state_amendyear state_amended state_constlength state_policy state_pid_match, estimate(mm) id(id) graph notable subgroup( t_twoparty )

* Table S9
use "data wide.dta", clear
reg vig_response vig_policyagree vig_divisive vig_constold
oprobit vig_response vig_policyagree vig_divisive vig_constold


